package com.tuoyu.peopletest.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;


import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.scinterface.OnDataListener;
import com.tuoyu.peopletest.utils.SCLogUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 *
 */
public class MyTestProgressView03 extends View {
    private final static String TAG = "MyTestProgressView03";

    private final Paint mPaintBg;

    private final Paint mPaintProgress;
    private int heightProgress;
    private int mStartX;
    private int mStartY;
    private int mEndX;
    private int mEndY;
    //进度条的x 坐标
    private float mPrgoressX;
    //图形高度
    private int mLineHeight = dip2px(20);
    //初始间距
    private int mStartSpace = dip2px(14);



    //总时间,
    private int mAllTime = 1 * 60 * 1000;
    private int mTime = 0;
    //刷新间隔
    private int mTimeSpace = 500;
    private int allLineWidth;
    //单位时间, 对应的图书距离
    private float spaceWidth;
    private String mShowTimeStr = "";
    private final Paint textPaint;
    Paint.FontMetrics fontMetrics;
    private final float baseLineTv;
    private float mTimeStrWidth;
    private float mTextStartX;
    private boolean isStart;

    public MyTestProgressView03(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        int colorBg = getResources().getColor(R.color.sc_c_bg_gray_97_63);
        int colorProgress = getResources().getColor(R.color.sc_c_bg_green);
        int colorText = getResources().getColor(R.color.sc_c_t_black);

        mPaintBg = new Paint();
        mPaintBg.setDither(true);
        mPaintBg.setAntiAlias(true);
        mPaintBg.setColor(colorBg);
        mPaintBg.setStyle(Paint.Style.FILL);
        //mPaintBg.setStrokeCap(Paint.Cap.ROUND);
        mPaintBg.setStrokeJoin(Paint.Join.ROUND);
        mPaintBg.setStrokeWidth(mLineHeight);

        mPaintProgress = new Paint();
        mPaintProgress.setDither(true);
        mPaintProgress.setAntiAlias(true);
        mPaintProgress.setColor(colorProgress);
        mPaintProgress.setStyle(Paint.Style.FILL);
        //mPaintProgress.setStrokeCap(Paint.Cap.ROUND);
        mPaintProgress.setStrokeJoin(Paint.Join.ROUND);
        mPaintProgress.setStrokeWidth(mLineHeight);
       // mHandler.sendEmptyMessageDelayed(0, 4000);

        textPaint = new Paint();
        textPaint.setDither(true);
        textPaint.setAntiAlias(true);
        textPaint.setColor(colorText);
        textPaint.setTextSize(sp2px(10));
        textPaint.setTextAlign(Paint.Align.CENTER);
        fontMetrics = textPaint.getFontMetrics();
        //y轴,文字基线
        baseLineTv = (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int height = getHeight();
        SCLogUtils.e(TAG, "布局高度=" + height);
        //heightProgress = (int) (getHeight() * 1f / 2);
        heightProgress = height;
        SCLogUtils.e(TAG, "布局高度 =" + height);
        //外边框

        //mStartX = mStartSpace;
        //开始划线起点
        mStartX = 0;
        mStartY = (int) (heightProgress * 1f / 2);
        //mEndX = getWidth() - mStartSpace;
        //线条的终点坐标
        mEndX = getWidth();
        mEndY = mStartY;

        allLineWidth = mEndX - mStartX;
        SCLogUtils.e(TAG, "allLineWidth =" + allLineWidth);
        mPrgoressX = mStartX;

        //单位时间, 图上对应的距离
        spaceWidth = allLineWidth * 1f / mAllTime * mTimeSpace;

        SCLogUtils.e(TAG, "spaceWidth =" + spaceWidth);
        SCLogUtils.e(TAG, "allLineWidth*1f/mAllTime  =" + allLineWidth * 1f / mAllTime);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //SCLogUtils.e(TAG, "onDraw mStartX=" + mStartX);
        canvas.drawLine(mStartX, mStartY, mEndX, mEndY, mPaintBg);
        canvas.drawLine(mStartX, mStartY, mPrgoressX, mEndY, mPaintProgress);
        canvas.drawText(mShowTimeStr, mTextStartX, mStartY + baseLineTv, textPaint);
        //SCLogUtils.e(TAG, "mPrgoressX =" + mPrgoressX);
    }

    public int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 轮训
     */
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (mPrgoressX >= mEndX) {
                mPrgoressX = mEndX;
                invalidate();
                stop();
                if(listener!=null){
                    listener.onData("");
                }
                return false;
            }
            mTime += mTimeSpace;
            //重新绘制
            mPrgoressX = mPrgoressX + spaceWidth;
            //文字
            if (mPrgoressX >= mEndX) {
                mPrgoressX = mEndX;
            }

            if (mTime > mAllTime) {
                mTime = mAllTime;
            }
            //计算文字
            mShowTimeStr = getSecToTime03(mTime);
            mTimeStrWidth = textPaint.measureText(mShowTimeStr);
            // mTextStartX = mPrgoressX - mTimeStrWidth * 1f / 2;
            mTextStartX = mPrgoressX + mTimeStrWidth * 1f / 2;
            if (mTextStartX <= 0) {
                mTextStartX = 0;
            }

            mTextStartX += dip2px(6);
            if (mTextStartX + mTimeStrWidth > mEndX) {
                mTextStartX = mEndX - mTimeStrWidth;
            }
            //SCLogUtils.e(TAG, "mTextStartX =" + mTextStartX);

            invalidate();
            mHandler.sendEmptyMessageDelayed(0, mTimeSpace);
            return false;
        }
    });

    /**
     * 毫秒时间转换为 字符串 时分秒
     * @param seconds
     * @return
     */
    public static String getSecToTime03(long seconds) {
        seconds = seconds / 1000;
        long hour = seconds / 3600;
        long minute = (seconds - hour * 3600) / 60;
        long second = (seconds - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            sb.append(hour);
            sb.append("时");
        }
        if (minute > 0) {
            sb.append(minute);
            sb.append("分");
        }
        if (second > 0) {
            sb.append(second);
            sb.append("秒");
        }
        return sb.toString();
    }

    public void setmAllTime(int mAllTime) {
        this.mAllTime = mAllTime;
    }

    public void start(){
        if(isStart){
            return;
        }
        isStart = true;
        mHandler.sendEmptyMessage(0);
    }



    public void stop() {
        isStart = false;
        mHandler.removeCallbacksAndMessages(null);

    }

    public void restore(){
        mPrgoressX = 0;
        mShowTimeStr = "";
        mTextStartX = 0;
        invalidate();
    }

    private OnDataListener<String> listener;
    public void setStopListener(OnDataListener<String> listener){
        this.listener = listener;
    }
}
